<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="商品订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入商品订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款单号" prop="outRefundNo">
        <el-input
          v-model="queryParams.outRefundNo"
          placeholder="请输入退款单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信退款单号" prop="refundId">
        <el-input
          v-model="queryParams.refundId"
          placeholder="请输入微信支付退款单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信支付订单号" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入微信支付订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="退款渠道" prop="channel">-->
<!--        <el-input-->
<!--          v-model="queryParams.channel"-->
<!--          placeholder="请输入退款渠道"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="退款入账账户" prop="userReceivedAccount">-->
<!--        <el-input-->
<!--          v-model="queryParams.userReceivedAccount"-->
<!--          placeholder="请输入退款入账账户"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="退款原因" prop="reason">-->
<!--        <el-input-->
<!--          v-model="queryParams.reason"-->
<!--          placeholder="请输入退款原因"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="订单总金额" prop="total">-->
<!--        <el-input-->
<!--          v-model="queryParams.total"-->
<!--          placeholder="请输入订单总金额，单位为分"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="退款金额" prop="refund">-->
<!--        <el-input-->
<!--          v-model="queryParams.refund"-->
<!--          placeholder="请输入退款金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="退款成功时间" prop="successTime">
        <el-date-picker clearable
                        v-model="queryParams.successTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择退款成功时间">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="用户支付金额" prop="payerTotal">-->
<!--        <el-input-->
<!--          v-model="queryParams.payerTotal"-->
<!--          placeholder="请输入用户支付金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="资金账户" prop="fundsAccount">-->
<!--        <el-input-->
<!--          v-model="queryParams.fundsAccount"-->
<!--          placeholder="请输入资金账户"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="用户退款金额" prop="payerRefund">-->
<!--        <el-input-->
<!--          v-model="queryParams.payerRefund"-->
<!--          placeholder="请输入用户退款金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="应结退款金额" prop="settlementRefund">-->
<!--        <el-input-->
<!--          v-model="queryParams.settlementRefund"-->
<!--          placeholder="请输入应结退款金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="应结订单金额" prop="settlementTota">-->
<!--        <el-input-->
<!--          v-model="queryParams.settlementTota"-->
<!--          placeholder="请输入应结订单金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="优惠退款金额" prop="discountRefund">-->
<!--        <el-input-->
<!--          v-model="queryParams.discountRefund"-->
<!--          placeholder="请输入优惠退款金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="退款币种" prop="currency">-->
<!--        <el-input-->
<!--          v-model="queryParams.currency"-->
<!--          placeholder="请输入退款币种"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="手续费退款金额" prop="refundFee">-->
<!--        <el-input-->
<!--          v-model="queryParams.refundFee"-->
<!--          placeholder="请输入手续费退款金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['refund:refund:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['refund:refund:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['refund:refund:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['refund:refund:export']"
        >导出
        </el-button>
      </el-col>
      <el-dropdown :hide-on-click="false" class="top-right-btn">
          <span class="el-dropdown-link">
            <el-button size="mini" icon="el-icon-setting" circle></el-button>
          </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-for="field in columns" :key="field.key">
            <el-checkbox :value="field.visible" @change="downChange(field.key)">{{ field.label }}</el-checkbox>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="refundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
<!--      <el-table-column label="主键" align="center" prop="id"/>-->
      <el-table-column label="商品订单号" align="center" prop="orderId" show-overflow-tooltip width="180" v-if="columns[0].visible"/>
      <el-table-column label="退款单号" align="center" prop="outRefundNo" show-overflow-tooltip width="180" v-if="columns[1].visible"/>
      <el-table-column label="微信支付退款单号" align="center" prop="refundId" show-overflow-tooltip width="220" v-if="columns[2].visible"/>
      <el-table-column label="微信支付订单号" align="center" prop="transactionId" show-overflow-tooltip width="220" v-if="columns[3].visible"/>
      <el-table-column label="退款原因" align="center" prop="reason" v-if="columns[4].visible"/>
      <el-table-column label="退款金额" align="center" prop="refund" v-if="columns[5].visible">
        <template slot-scope="scope">
          {{ (scope.row.refund / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="退款状态" align="center" prop="status" width="200" v-if="columns[6].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.refund_state" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="退款成功时间" align="center" prop="successTime" width="200" v-if="columns[7].visible">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.successTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款渠道" align="center" prop="channel" v-if="columns[8].visible"/>
      <el-table-column label="退款入账账户" align="center" prop="userReceivedAccount" v-if="columns[9].visible"/>
      <el-table-column label="订单总金额" align="center" prop="total" v-if="columns[10].visible">
        <template slot-scope="scope">
          {{ (scope.row.total / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="用户支付金额" align="center" prop="payerTotal" v-if="columns[11].visible">
        <template slot-scope="scope">
          {{ (scope.row.payerTotal / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="资金账户" align="center" prop="fundsAccount" v-if="columns[12].visible"/>
      <el-table-column label="用户退款金额" align="center" prop="payerRefund" v-if="columns[13].visible">
        <template slot-scope="scope">
          {{ (scope.row.payerRefund / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="应结退款金额" align="center" prop="settlementRefund" v-if="columns[14].visible">
        <template slot-scope="scope">
          {{ (scope.row.settlementRefund / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="应结订单金额" align="center" prop="settlementTota" v-if="columns[15].visible">
        <template slot-scope="scope">
          {{ (scope.row.settlementTota / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="优惠退款金额" align="center" prop="discountRefund" v-if="columns[16].visible">
        <template slot-scope="scope">
          {{ (scope.row.discountRefund / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="退款币种" align="center" prop="currency" v-if="columns[17].visible"/>
      <el-table-column label="手续费退款金额" align="center" prop="refundFee" v-if="columns[18].visible">
        <template slot-scope="scope">
          {{ (scope.row.refundFee / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['refund:refund:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['refund:refund:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退款订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品订单号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入商品订单号"/>
        </el-form-item>
        <el-form-item label="退款单号" prop="outRefundNo">
          <el-input v-model="form.outRefundNo" placeholder="请输入退款单号"/>
        </el-form-item>
        <el-form-item label="微信支付退款单号" prop="refundId">
          <el-input v-model="form.refundId" placeholder="请输入微信支付退款单号"/>
        </el-form-item>
        <el-form-item label="微信支付订单号" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入微信支付订单号"/>
        </el-form-item>
        <el-form-item label="退款渠道" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入退款渠道"/>
        </el-form-item>
        <el-form-item label="退款入账账户" prop="userReceivedAccount">
          <el-input v-model="form.userReceivedAccount" placeholder="请输入退款入账账户"/>
        </el-form-item>
        <el-form-item label="退款原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入退款原因"/>
        </el-form-item>
        <el-form-item label="订单总金额" prop="total">
          <el-input v-model="form.total" placeholder="请输入订单总金额，单位为分"/>
        </el-form-item>
        <el-form-item label="退款金额" prop="refund">
          <el-input v-model="form.refund" placeholder="请输入退款金额"/>
        </el-form-item>
        <el-form-item label="退款成功时间" prop="successTime">
          <el-date-picker clearable
                          v-model="form.successTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择退款成功时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户支付金额" prop="payerTotal">
          <el-input v-model="form.payerTotal" placeholder="请输入用户支付金额"/>
        </el-form-item>
        <el-form-item label="资金账户" prop="fundsAccount">
          <el-input v-model="form.fundsAccount" placeholder="请输入资金账户"/>
        </el-form-item>
        <el-form-item label="用户退款金额" prop="payerRefund">
          <el-input v-model="form.payerRefund" placeholder="请输入用户退款金额"/>
        </el-form-item>
        <el-form-item label="应结退款金额" prop="settlementRefund">
          <el-input v-model="form.settlementRefund" placeholder="请输入应结退款金额"/>
        </el-form-item>
        <el-form-item label="应结订单金额" prop="settlementTota">
          <el-input v-model="form.settlementTota" placeholder="请输入应结订单金额"/>
        </el-form-item>
        <el-form-item label="优惠退款金额" prop="discountRefund">
          <el-input v-model="form.discountRefund" placeholder="请输入优惠退款金额"/>
        </el-form-item>
        <el-form-item label="退款币种" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入退款币种"/>
        </el-form-item>
        <el-form-item label="手续费退款金额" prop="refundFee">
          <el-input v-model="form.refundFee" placeholder="请输入手续费退款金额"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addRefund, delRefund, getRefund, listRefund, updateRefund} from "@/api/refund/refund";

export default {
  name: "Refund",
  dicts: ['sys_normal_disable', 'refund_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退款订单表格数据
      refundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        outRefundNo: null,
        refundId: null,
        transactionId: null,
        channel: null,
        userReceivedAccount: null,
        reason: null,
        total: null,
        refund: null,
        successTime: null,
        payerTotal: null,
        fundsAccount: null,
        status: null,
        payerRefund: null,
        settlementRefund: null,
        settlementTota: null,
        discountRefund: null,
        currency: null,
        refundFee: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 列信息
      columns: [
        {key: 0, label: `商品订单号`, prop: 'orderId', visible: true},
        {key: 1, label: `微信支付退款单号`, prop: 'refundId', visible: false},
        {key: 2, label: `微信支付订单号`, prop: 'transactionId', visible: false},
        {key: 3, label: `退款单号`, prop: 'outRefundNo', visible: false},
        {key: 4, label: `退款原因`, prop: 'reason', visible: true},
        {key: 5, label: `退款金额`, prop: 'refund', visible: true},
        {key: 6, label: `退款状态`, prop: 'status', visible: true},
        {key: 7, label: `退款成功时间`, prop: 'successTime', visible: true},
        {key: 8, label: `退款渠道`, prop: 'channel', visible: false},
        {key: 9, label: `退款入账账户`, prop: 'userReceivedAccount', visible: false},
        {key: 10, label: `订单总金额`, prop: 'total', visible: false},
        {key: 11, label: `用户支付金额`, prop: 'payerTotal', visible: false},
        {key: 12, label: `资金账户`, prop: 'fundsAccount', visible: false},
        {key: 13, label: `用户退款金额`, prop: 'payerRefund', visible: false},
        {key: 14, label: `应结退款金额`, prop: 'settlementRefund', visible: false},
        {key: 15, label: `应结订单金额`, prop: 'settlementTota', visible: false},
        {key: 16, label: `优惠退款金额`, prop: 'discountRefund', visible: false},
        {key: 17, label: `退款币种`, prop: 'currency', visible: true},
        {key: 18, label: `手续费退款金额`, prop: 'refundFee', visible: false},
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    downChange(val) {
      const column = this.columns.find(col => col.key === val);
      if (column) {
        column.visible = !column.visible;
      }
    },
    /** 查询退款订单列表 */
    getList() {
      this.loading = true;
      listRefund(this.queryParams).then(response => {
        this.refundList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        orderId: null,
        outRefundNo: null,
        refundId: null,
        transactionId: null,
        channel: null,
        userReceivedAccount: null,
        reason: null,
        total: null,
        refund: null,
        successTime: null,
        payerTotal: null,
        fundsAccount: null,
        status: null,
        payerRefund: null,
        settlementRefund: null,
        settlementTota: null,
        discountRefund: null,
        currency: null,
        refundFee: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退款订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRefund(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退款订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRefund(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRefund(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除退款订单编号为"' + ids + '"的数据项？').then(function () {
        return delRefund(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('refund/refund/export', {
        ...this.queryParams
      }, `refund_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
